﻿using DL.Initializers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DL.Repositories
{
    public class BinhLuanRepository
    {
        private SqlConnection _connect;
        private SqlDataAdapter _sqlAdapter;
        public string CapNhatBinhLuan(BinhLuan bl)
        {
            try
            {
                if (bl.Id != 0)
                {
                    if (bl.NoiDung != null)
                    {
                        //Update binh luan
                    }
                    else
                    {
                        //Delete binh luan
                        ConnectDataBase.CloseConnection(_connect);
                        _connect = new SqlConnection();
                        _connect = ConnectDataBase.GetSQlConnection(_connect);
                        var cm = _connect.CreateCommand();
                        cm.CommandText = "dbo.C_DELETE_BINHLUAN";
                        cm.CommandType = CommandType.StoredProcedure;
                        cm.Parameters.Add("@id", SqlDbType.BigInt).Value = bl.Id;
                        cm.Parameters.Add("@NewQuoteNumber", SqlDbType.Int).Direction = ParameterDirection.Output;
                        //cm.Parameters.Add(newReqNumber);
                        cm.ExecuteNonQuery();
                        string NewQuoteNumber = cm.Parameters["@NewQuoteNumber"].Value.ToString();
                        if (NewQuoteNumber == "0" || NewQuoteNumber == "") throw new Exception();
                    }
                }
                else
                {
                    if (bl.IdBinhLuanCha == 0)
                    {
                        ConnectDataBase.CloseConnection(_connect);
                        _connect = new SqlConnection();
                        _connect = ConnectDataBase.GetSQlConnection(_connect);
                        var cm = _connect.CreateCommand();
                        cm.CommandText = "dbo.C_INSERT_BINHLUAN";
                        cm.CommandType = CommandType.StoredProcedure;
                        cm.Parameters.Add("@noidung", SqlDbType.NVarChar).Value = bl.NoiDung;
                        cm.Parameters.Add("@trang_thai", SqlDbType.Int).Value = bl.TrangThai;
                        cm.Parameters.Add("@sanpham", SqlDbType.BigInt).Value = bl.SanPhamId;
                        cm.Parameters.Add("@NewQuoteNumber", SqlDbType.BigInt).Direction = ParameterDirection.Output;
                        cm.ExecuteNonQuery();
                        string NewQuoteNumber = cm.Parameters["@NewQuoteNumber"].Value.ToString();
                        if (NewQuoteNumber == "0" || NewQuoteNumber == "") throw new Exception();
                    }
                    else
                    {
                        ConnectDataBase.CloseConnection(_connect);
                        _connect = new SqlConnection();
                        _connect = ConnectDataBase.GetSQlConnection(_connect);
                        var cm = _connect.CreateCommand();
                        cm.CommandText = "dbo.C_INSERT_BINHLUAN_TRALOI";
                        cm.CommandType = CommandType.StoredProcedure;
                        cm.Parameters.Add("@noidung", SqlDbType.NVarChar).Value = bl.NoiDung;
                        cm.Parameters.Add("@trang_thai", SqlDbType.Int).Value = bl.TrangThai;
                        cm.Parameters.Add("@sanpham", SqlDbType.BigInt).Value = bl.SanPhamId;
                        cm.Parameters.Add("@binh_luan_cha", SqlDbType.BigInt).Value = bl.IdBinhLuanCha;
                        cm.Parameters.Add("@NewQuoteNumber", SqlDbType.BigInt).Direction = ParameterDirection.Output;
                        cm.ExecuteNonQuery();
                        string NewQuoteNumber = cm.Parameters["@NewQuoteNumber"].Value.ToString();
                        if (NewQuoteNumber == "0" || NewQuoteNumber == "") throw new Exception();
                    }
                }
                return "Thành công";
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                ConnectDataBase.CloseConnection(_connect);
            }
        }
    }
}
